package com.base.caselib.presenter;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.base.caselib.view.UpCaseView;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.bean.RoadEntity;
import com.base.commonlib.utils.MineRoadUtil;

import java.util.List;

/**
 * Created by weikailiang on 2019/11/1.
 */

public class UpCasePresenter extends BasePresenter<UpCaseView> implements AMapLocationListener {
    public List<RoadEntity> mMMineRoads;
    public List<RoadEntity> mRoadEntities;
    public UpCasePresenter(UpCaseView view, Activity activity) {
        super(view, activity);
    }
    /**
     * 声明mlocationClient对象
     */
    private AMapLocationClient mLocationClient;
    /**
     * 声明mLocationOption对象
     */
    public AMapLocationClientOption mLocationOption = null;

    @Override
    public void start() {
        super.start();
        mMMineRoads = MineRoadUtil.getMineRoads(mUser.getUserId());
        mRoadEntities = MineRoadUtil.getRoads();
        startLocationForLl();
    }
    public void startLocationForLl(){
        mLocationClient = new AMapLocationClient(mActivity);
        mLocationOption = new AMapLocationClientOption();
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setOnceLocation(true);
        mLocationOption.setOnceLocationLatest(true);
        mLocationOption.setHttpTimeOut(8000);
        mLocationOption.setNeedAddress(true);
        mLocationOption.setLocationCacheEnable(true);
        // 设置定位监听
        mLocationClient.setLocationListener(this);
        // 设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        startMapLocation();
    }
    /**
     * 开启定位
     */
    public void startMapLocation() {
        //判断是否开启了，没有开启就开启
        if (!mLocationClient.isStarted()) {
            mLocationClient.startLocation();
        }
    }

    /**
     * 停止定位服务
     */
    public void stopMapLocation() {
        //判断服务是否开启了，若开启了则停止
        if (mLocationClient.isStarted()) {
            mLocationClient.stopLocation();
        }
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                // 可在其中解析amapLocation获取相应内容。
                //给接口设置数据
                if (TextUtils.isEmpty(aMapLocation.getAddress())||aMapLocation.getAddress() == null){
                    return;
                }
                mView.returnLocation(aMapLocation);
                stopMapLocation();
            } else {
                mView.showMes("定位权限未开启");
                //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
                stopMapLocation();
            }
        } else {
            stopMapLocation();
        }
    }


    @Override
    public void release() {
        super.release();
        if (mLocationClient != null) {
            mLocationClient.stopLocation();
            mLocationClient.onDestroy();
        }
    }
}
